﻿Imports MySql.Data.MySqlClient
Imports System.Data

Public Class CContext
    Protected MySqlConnect As MySqlConnection

    Public Sub New()
        Me.MySqlConnect = New MySqlConnection(My.Settings.KoneksiString)
    End Sub

    Protected Sub InsertData(ByVal query As String)
        MySqlConnect.Open()
        Dim mysqlInsertCommand As MySqlCommand = MySqlConnect.CreateCommand
        Try
            With mysqlInsertCommand
                .CommandText = query
                .ExecuteNonQuery()
            End With
        Catch ex As Exception
            Throw ex
        End Try
        MySqlConnect.Close()
    End Sub

    Protected Sub InsertData(ByVal InsertCommand As MySqlCommand)
        MySqlConnect.Open()
        Try
            InsertCommand.Connection = MySqlConnect
            InsertCommand.ExecuteNonQuery()
        Catch ex As Exception
            Throw ex
        End Try
        MySqlConnect.Close()
    End Sub

    Protected Sub UpdateData(ByVal query As String)
        MySqlConnect.Open()
        Dim mysqlUpdateCommand As MySqlCommand = MySqlConnect.CreateCommand
        Try
            With mysqlUpdateCommand
                .CommandText = query
                .ExecuteNonQuery()
            End With
        Catch ex As Exception
            Throw ex
        End Try
        MySqlConnect.Close()
    End Sub

    Protected Sub UpdateData(ByVal UpdateCommand As MySqlCommand)
        MySqlConnect.Open()
        Try
            UpdateCommand.Connection = MySqlConnect
            UpdateCommand.ExecuteNonQuery()
        Catch ex As Exception
            Throw ex
        End Try
        MySqlConnect.Close()
    End Sub

    Protected Sub DeleteData(ByVal query As String)
        MySqlConnect.Open()
        Dim mysqlDeleteCommand As MySqlCommand = MySqlConnect.CreateCommand
        Try
            With mysqlDeleteCommand
                .CommandText = query
                .ExecuteNonQuery()
            End With
        Catch ex As Exception
            Throw ex
        End Try
        MySqlConnect.Close()
    End Sub

    Protected Function SelectData(ByVal query As String) As DataTable
        Dim dTab As New DataTable

        Try
            MySqlConnect.Open()
            Dim mysqlSelectCommand As MySqlCommand = MySqlConnect.CreateCommand

            With mysqlSelectCommand
                .CommandText = query
            End With

            Dim mysqlAdapt As New MySqlDataAdapter(mysqlSelectCommand)

            mysqlAdapt.Fill(dTab)

            MySqlConnect.Close()


        Catch mEx As MySqlException
            If mEx.Message.Contains("Unable to connect") Then
                If MessageBox.Show("Pusat data tidak aktif. Mau diaktifkan?", "Peringatan", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.Yes Then
                    'aktifkan database
                    'Process.Start("c:\xampp\mysql_start.bat")
                    Dim startDb As New Process
                    startDb.StartInfo.FileName = "C:\xampp\mysql_start.bat"
                    startDb.Start()

                Else
                    Throw mEx
                End If
            Else
                Throw mEx
            End If

        Catch ex As Exception
            Throw ex
        End Try

        Return dTab
    End Function

End Class
